<?php
include_once 'Element.php';
include_once 'SelectGroup.php';
include_once 'SelectOption.php';

/**
 * Elemento Select HTML
 */
class Select extends Element {

    private $multiple;
    //properties
    private $selected = '';
    private $width;
    
    //events
    private $onChange;

    /**
     * @var SelectOption
     */
    private $options;

    /**
     * @var SelectGroup
     */
    private $groups = array();

    /**
     * Constructor
     * @param String $id Identificador del Select
     */
    public function __construct($id) {
        parent::__construct($id);
    }

    /**
     * Genera Código HTMl
     * @return String HTML
     */
    public function generate() {
        $html = array();
        $params = '';
        if ($this->multiple)
            $params = $params . 'multiple';

        $html[] = '<select ' . $params . ' onchange="' . $this->onChange . '" id="' . parent::getId() . '" name="' . parent::getId() . '">';

        if (count($this->groups)) {
            $groups = $this->groups;
            foreach ($groups as $group) {
                $html[] = (String) $group;
            }
        } else {
            $options = $this->options;
            if (count($options)) {
                foreach ($options as $option) {
                    $html[] = (String) $option;
                }
            }
        }

        $html[] = '</select>';

        $output = '';
        foreach ($html as $code) {
            $output = $output . $code;
        }
        return $output;
    }

    /**
     * Agrega un Item al Select
     * @param SelectOption $option Objeto Opción
     */
    public function addOption(SelectOption $option) {
        $this->options[] = $option;
    }

    /**
     * Agrega un Grupo al Select
     * @param SelectGroup $group Objeto Grupo
     */
    public function addGroup(SelectGroup $group) {
        $this->groups[] = $group;
    }

    /**
     * Obtiene el valor de Múltiple del Select
     * @return Boolean
     */
    public function getMultiple() {
        return $this->multiple;
    }

    /**
     * Establece el valor de Múltiple del Select
     * @param Boolean $multiple
     */
    public function setMultiple($multiple) {
        $this->multiple = $multiple;
    }

    /**
     * Obtiene el valor de Selected del Select
     * @return Boolean
     */
    public function getSelected() {
        return $this->selected;
    }

    /**
     * Establece el valor de Selected del Select
     * @param Boolean $selected
     */
    public function setSelected($selected) {
        $this->selected = $selected;
    }

    /**
     * Obtiene el valor ancho del Select en Pixeles
     * @return String
     */
    public function getWidth() {
        return $this->width;
    }

    /**
     * Establece el valor de ancho del Select en Pixeles
     * @param type $width
     */
    public function setWidth($width) {
        $this->width = $width;
    }

    /**
     * Obtiene el código javascript del evento que se ejecuta al seleccionar una opcion del Select
     * @return String
     */
    public function getOnChange() {
        return $this->onChange;
    }

    /**
     * Establece el Evento que se ejecuta al seleccionar una opcion del select
     * @param String $onChange text/javascript
     */
    public function setOnChange($onChange) {
        $this->onChange = $onChange;
    }

}

?>
